package zpin.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

import zpin.dao.AuthorizationDao;
import zpin.dao.CompanyDao;
import zpin.model.Authorization;
import zpin.model.Company;
import zpin.model.Role;

import com.opensymphony.xwork2.ActionSupport;

public class CompanyAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long id;

	private Company company;

	private Authorization authorization;

	private CompanyDao companyDao;

	private AuthorizationDao authorizationDao;

	List<Company> companylist = new ArrayList<Company>();

	public List<Company> getCompanylist() {
		return companylist;
	}

	public void setCompanylist(List<Company> companylist) {
		this.companylist = companylist;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Authorization getAuthorization() {
		return authorization;
	}

	public void setAuthorization(Authorization authorization) {
		this.authorization = authorization;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public AuthorizationDao getAuthorizationDao() {
		return authorizationDao;
	}

	public void setAuthorizationDao(AuthorizationDao authorizationDao) {
		this.authorizationDao = authorizationDao;
	}

	public String update() {
		Company temp = new Company();
		temp = (Company) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		company.setAuthorization(temp.getAuthorization());
		companyDao.update(company);
		return SUCCESS;
	}

	public void validateUpdate() {
		if (!company.getName().equals(
				companyDao.findById(company.getId()).getName())) {
			if (!companyDao.isUniqueName(company.getName())) {
				this.addFieldError("company.name", "该公司名已存在");
			}
		}
	}

	public String input() {
		return "input";
	}

	public String execute() {
		return SUCCESS;
	}

	public String create() {
		return SUCCESS;
	}

	public String select() {
		Company temp = new Company();
		temp = (Company) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		company = companyDao.findById(temp.getId());
		return SUCCESS;
	}

	public String change() {
		Company temp = (Company) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		authorization = temp.getAuthorization();
		return SUCCESS;
	}

	public String updatecode() {
		Company temp = (Company) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		authorization.setAccount(temp.getAuthorization().getAccount());
		Set<Role> roleSet = new HashSet<Role>();
		roleSet.add(temp.getAuthorization().getRoles().iterator().next());
		authorization.setRoles(roleSet);
		authorizationDao.update(authorization);
		return SUCCESS;
	}
}
